from pathlib import Path
from hashlib import sha256
from collections import defaultdict
from random import choice

class S:
    BLUE = '\033[94m'
    CYAN = '\033[96m'
    GREEN = '\033[92m'
    NOR = '\033[0m'

    def print(s, *args):
        print(s.format(S.BLUE, S.NOR, *args))

def f(s: str, p = Path.cwd()):                  # simulate three files
    ps = [p / f'{s}-a.m', p / f'{s}-b.m', p / f'{s}-c.m']
    ps += ([p.with_suffix('.c') for p in ps] + [p.with_suffix('.h') for  p in ps])
    w = ['hi', 'yo']
    # 1. simulate the source file
    # ps[0].write_text('hi')
    # ps[1].write_text('yo')
    # ps[2].write_text('hi')
    for p in ps:
        p.write_text(choice(w))

    # 2. get the hashes
    h = lambda x: sha256(x.read_text().encode()).hexdigest()
    hs = [h(p) for p in ps]

    o = defaultdict(list)
    for h, p in zip(hs, ps):
        o[h].append(p.as_posix()) if p.suffix == '.m' else None

    return f'{s}.m', o

tmp = Path.cwd() / '.tmp'
tmp.mkdir() if not tmp.exists() else None
o = {}
# 01. make dir
for i in range(3):
    s, r = f(f'f{i}', tmp)
    o[s] = r

# 02. store json
import json
p0 = tmp / Path('db.json')
p0.write_text(json.dumps(o, indent=4))
S.print('🦜 : db file dump to {0}{2}{1}', p0)

# 03. show the json
S.print('🦜 : db: {0}{2}{1}', p0.read_text())

# --------------------------------------------------
# <2025-05-29 Thu> Make more mocked files
# 4.1.
d = {f'mocked_f{i}c' : f'<obj for mocked f{i}c>' for i in range(4)}
p0 = tmp / Path('mocked_c_api_doc.json')
p0.write_text(json.dumps(d, indent=4))
S.print('🦜 : mocked c api doc file dump to {0}{2}{1}', p0)

d = {f'mocked_f{i}m' : f'<obj for mocked f{i}m>' for i in range(4)}
p0 = tmp / Path('mocked_matlab_api_doc.json')
p0.write_text(json.dumps(d, indent=4))
S.print('🦜 : mocked matlab api doc file dump to {0}{2}{1}', p0)


# the mocked template
s = '''
matlab name:
{m_name}

matlab code:
{m_code}

header name:
{h_name}

header code:
{h_code}

matlab doc:
{m_doc}

c doc:
{c_doc}

rendered_headers:
{rendered_headers}

c name:
{c_name}
'''
p0 = tmp / Path('mocked-template.txt')
p0.write_text(s)
